// const os = require('node:os')
import os from 'os'
import path from 'path'
import { fileURLToPath } from 'url'

// url 文件绝对路径
const __filename = (url) => fileURLToPath(url)

// url 文件所在目录
const __dirname = url=>path.dirname(__filename(url))

// 获取当前机器的ip地址(仅在 Windows 系统下生效)
const getIpAddress = () => {
  var ifaces = os.networkInterfaces()
  console.log(ifaces)
  let addressStr = ""
  for (let key in ifaces) {
    if (key && ifaces[key] && ifaces[key].length > 0) {
      for (let item of ifaces[key]) {
        const { family, address, internal } = item
        if (key.includes("本地连接") && family === 'IPv4' && address !== "127.0.0.1" && !internal) {
          addressStr = address
          return addressStr
        }
      }
    }
  }
  return addressStr
}

// 获取与根文件夹同级的其他文件夹
const getSameRootFolder = path => {
  if (path && typeof path === "string") {
    let str = `\\`
    if (os.type() === "Darwin") str = "/"
    // Windows_NT 为 windows
    // Linux 为 linux
    // Darwin 为 Mac
    const folders = process.cwd().split(str)
    folders[folders.length - 1] = path
    path = folders.join(str) + str
  }
  return path
}

const __wallPaperPath = getSameRootFolder("WallPaper")

export {
  __filename,
  __dirname,
  __wallPaperPath,
  getIpAddress
}